#!/bin/bash

# Regular Expression (or regex) are used in many *nix commands including sed.

# * ^ matches at the start of a line
sed -n '/^103/p' ../sed/test_employee.txt # prints the line start with '103'
echo -e '\n'


# * '$' matches the end of a line 
sed -n '/r$/ p' ../sed/test_employee.txt # prints a line with r end of it.
echo -e '\n'


# * '.'(dot) matches any character except the end of line character.
#   * '.' matches single character
#   * '..' matches two characters
#   ...
sed -n 's/J... /Jason /p' ../sed/test_employee.txt # substitude any letter with 'Jxxx' form into Jason and print the line.
echo -e '\n'


# * '*' matches zero or more occurrences of the previous character.
#   e.g. '1*' matches zero or more '1'

# * print the lines only contains 'log:' and followd by contents
sed -n '/log: *./ p' ./test_log.txt
echo -e '\n'
#   * /log: *./ matches the pattern: log:<contents> and log: <contents>


# * '\+' back-slash, matches one or more occurrence of the previous character
#   i.e. " \+" matches at least one or more space character
sed -n '/log: \+/ p' ./test_log.txt # matches the pattern: log: <contents> or log:    <content>
echo -e '\n'


# * "\?" back-slash, matches zero or one occurrences of the previous character 
sed -n '/log: \?/ p' ./test_log.txt # matches the pattern: log <contents> , log:<contents> or log:
echo -e '\n'


# * use "\" back-slash to escaping the special character in regular expression
#   i.e. to represent '.' use '\.'
sed -n '/127\.0\.0\.1/ p' /etc/hosts
echo -e '\n'



#-- Character Class --
# the caracter class is nothing but a list of characters mentiond within a square bracket. this is used to match only one out of several characters

sed -n '/[234]/ p' ../sed/test_employee.txt # prints 102, 103, 104
echo -e '\n'

# * give a range of search number
sed -n '/[2-4]/ p' ../sed/test_employee.txt
echo -e '\n'
